package com.ykdl.member.kid.regandlogin;

import java.util.HashMap;
import java.util.Map;

import oauth.signpost.http.HttpParameters;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import net.wxxr.http.util.Constants;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.umeng.analytics.MobclickAgent;
import com.ykdl.member.kid.R;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.beans.RegisterPhonebean;
import com.ykdl.member.kid.beans.ResetPasswordBean;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.gears.HomeActivity;
import com.ykdl.member.kid.regandlogin.ForgetPwOneActivity.resetpasswordTag;
import com.ykdl.member.kid.util.AccessTokenKeeper;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

public class ForgetPwTwoActivity extends BaseScreen{
	private  EditText edit_pw_one,edit_pw_two ;
	String phonesString = "" ;
	String verifyCode = "" ;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTopTitle("重设密码", 0, R.drawable.back_bg, new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				finish();
			}
		}, R.string.turn_ok, 0, new OnClickListener() {
			@Override
			public void onClick(View v) {
				checkPassWord();
			}
		}, R.drawable.title_bg, false);
		setContent(R.layout.forgetpwtwoactivity_xml);
		phonesString = this.getIntent().getStringExtra("phone_number");
		verifyCode = this.getIntent().getStringExtra("verifyCode");
		inint();
		
	}
	public void inint() {
		edit_pw_one = (EditText)findViewById(R.id.edit_pw_one);
		edit_pw_two = (EditText)findViewById(R.id.edit_pw_two);
	}
	public void checkPassWord() {
		String passwordone = edit_pw_one.getText().toString();
		String passwordtwo = edit_pw_two.getText().toString();
		
		if (!passwordone.equals(passwordtwo)) {
			Toast.makeText(ForgetPwTwoActivity.this, "2次密码不一致", 1).show();
			return;
		}else if(passwordone.length()<6||passwordone.length()>16||passwordtwo.length()<6||passwordtwo.length()>16){
			Toast.makeText(ForgetPwTwoActivity.this, "密码格式不对", 1).show();
			return;
		}else {
			
			verifyCode(passwordone);
		}
	}
	public void verifyCode (String pw) {
		showProgress("正在发送请稍等...");
		String  pageurl = KidConfig.RESET_PASSWORD;
		HttpParameters bodyParams = new HttpParameters();
		bodyParams.put("confirm_code", verifyCode);
		bodyParams.put("password", pw);
		bodyParams.put("phone", phonesString);
		HttpBaseRequest request  =  Wxxr.getInstance().postBaseRequest(pageurl,bodyParams, null);
		 TaskManager.startHttpRequest(request, new resetpasswordTag(),  ResetPasswordBean.class);
		
	}
	class resetpasswordTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			if (t instanceof ResetPasswordBean) {
				finishProgress();
				ResetPasswordBean bean = (ResetPasswordBean)t;
				if (bean.getError()!=null) {
						Toast.makeText(mContext, "失败"+bean.getDesc(), 1).show();
				}else {
					if (bean.getStatus()==0) {
						Toast.makeText(mContext, "成功", 1).show();
						goNativeLogin();
						
						
					}else {
						Toast.makeText(mContext, "失败"+bean.getStatus(), 1).show();
					}
					
				}
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
	public void goNativeLogin() {
		showProgress("正在登录请稍等...");
		String pageurl = KidConfig.GET_ACCESS_TOKEN;
		HttpParameters bodyParams = new HttpParameters();
		bodyParams.put("client_id", Constants.CLIENT_ID);
		bodyParams.put("client_secret", Constants.CLIENT_SECRET);
		bodyParams.put("identity", phonesString);
		bodyParams.put("password", edit_pw_two.getText().toString());
		bodyParams.put("grant_type", "password");
		HttpBaseRequest request = Wxxr.getInstance().postBaseRequest(pageurl,
				bodyParams, null);
		TaskManager.startHttpRequest(request, new goRegisterTag(),
				RegisterPhonebean.class);
	}
	class goRegisterTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			Log.e("alan", "------------goRegisterTag:" + t.toString());
			if (t instanceof RegisterPhonebean) {
				finishProgress();
				RegisterPhonebean bean = (RegisterPhonebean) t;
				if (bean.getDesc() != null) {
					Toast.makeText(mContext, bean.getDesc(), 1).show();
				} else {
					AccessTokenKeeper.keepAccessToken(ForgetPwTwoActivity.this, bean);
						Toast.makeText(mContext, "登录成功", 1).show();
						MobclickAgent.onEvent(ForgetPwTwoActivity.this,
								"register_users");
						go(HomeActivity.class);
						ForgetPwTwoActivity.this.finish();

				}

				Log.e("alan",
						"------------Access_token:" + bean.getAccess_token());
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "失败", 1).show();
			return true;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub

		}

	}
}
